﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_Module_Category_Controls_CategoryAE : System.Web.UI.UserControl
{
    public Guid CategoryId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["CategoryId"] != null)
        {
            CategoryId = new Guid(Request["CategoryId"].ToString());
            btnUpdate.Text = "Cập nhật";
        }
        else CategoryId = Guid.Empty;
        if (!IsPostBack)
        {
            LoadDLL();
            LoadData();
        }

    }
    private void LoadDLL()
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add("Name ASC");
        ddlParent.DataSource = new TblCategoryRepository().SelectListForDDL(null, 0);
        ddlParent.DataTextField = "Name";
        ddlParent.DataValueField = "Id";
        ddlParent.DataBind();
    }
    private void LoadData()
    {
        if (!CategoryId.Equals(Guid.Empty))
        {
            IList<TblCategory> ls = new TblCategoryRepository().SelectListItem(CategoryId, "", "", null, null, null, new HashSet<string>());

            if (ls != null && ls.Count > 0)
            {
                TblCategory item = ls[0];
                txtName.Text = item.Name;
                ddlParent.SelectedValue = item.ParentId.HasValue ? item.ParentId.Value.ToString() : "";
                cbIsVisible.Checked = item.IsVisible;
                cbIsPublish.Checked = item.IsPublish;
                lblCreatedBy.Text = item.CreatedBy;
                lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
                lblUpdatedBy.Text = item.UpdatedBy;
                lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
            }
        }
    }
    private TblCategory GetTblCategory()
    {
        TblCategory obj = new TblCategoryRepository().SelectOne(CategoryId);
        if (obj == null)
        {
            obj = new TblCategory();
            obj.Id = Guid.NewGuid();
            obj.TextId = new TblCategoryRepository().GetUniqueTextIdFromUnicodeText(txtName.Text, 250);
            obj.CreatedBy = HttpContext.Current.User.Identity.Name;
            obj.CreatedDate = DateTime.Now;
            obj.DisplayOrder = 100;
        }
        if (ddlParent.SelectedValue.Equals(""))
            obj.ParentId = null;
        else
            obj.ParentId = new Guid(ddlParent.SelectedValue.ToString());
        obj.Name = txtName.Text;
        obj.IsVisible = cbIsVisible.Checked;
        obj.IsPublish = cbIsPublish.Checked;
        obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
        obj.UpdatedDate = DateTime.Now;

        return obj;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (CategoryId.Equals(Guid.Empty))
            new TblCategoryRepository().Insert(GetTblCategory());
        else
            new TblCategoryRepository().Update(GetTblCategory());
        Response.Redirect("CategoryList.aspx");
    }
}
